package com.jerry.walking.module.activityTravel.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.jerry.walking.R;
import com.jerry.walking.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Yang on 2016/5/9.
 *
 *          游记主界面
 *
 */
public class ActivityTravelFragment extends BaseFragment{

    @BindView(R.id.rg_travel_1)
    RadioGroup radioGroup;

    @BindView(R.id.vp_travel_1)
    ViewPager viewPager;

    //存放viewpaget中的fragment集合
    private List<Fragment> fragmentList;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_travel_activity;
    }

    @Override
    protected void initViews() {
        super.initViews();
        fragmentList = new ArrayList<>();
        fragmentList.add(new LiveFragment());
        fragmentList.add(new RecommendFragment());

        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                return super.instantiateItem(container, position);
            }
        });



        //添加 rg 监听器
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_travel_1:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.rb_travel_2:
                        viewPager.setCurrentItem(1);
                        break;
                }
            }
        });


        //添加 vp 监听器
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        radioGroup.check(R.id.rb_travel_1);
                        break;

                    case 1:
                        radioGroup.check(R.id.rb_travel_2);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
